﻿
<script type="text/javascript">
    function login(){
        alert('Please login to download documents!');
        return false;
    }	
</script>


<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
?>
<div class="clear"></div>
<div class="taj mt20 ofh">
<form action="" method="post">
    <?php
	$act = $_GET['act'];
	if($act == 'down')
	{
		$db=& JFactory::getDBO(); 
        $dblink=& JFactory::getDBO();

		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		$query="select * from #__search_content where id=". $id;
        $db->setQuery($query);
        $data=$db->loadObject();
		
		if(!empty($data))
		{
			exportpdf($data->descrip);
			exit;
		}
	}
        $db=& JFactory::getDBO(); 
        $dblink=& JFactory::getDBO();
        if (!isset($_POST['searchfilter']) && !isset($_POST['download'])){
		$_SESSION['descrip']="";
		$_SESSION['nd']="";
    ?>  
	    <div class="w385 h220 bg3 mr5 ofh fl pa15">
        <?php
        	$query="select * from #__search_school where status='1' order by name asc";
            $db->setQuery($query);
            $row=$db->loadObjectList();
            if($db->getErrorNum()){
                echo $db->stderr();
                return $false;
            }  
        ?>
            <p class="title fs18 ttu"><a href="">School</a></p>
			<?php
                    for($i=0,$n=count($row);$i<$n;$i++)
				    {
					   $rows=&$row[$i];
                       $id=$rows->id;
                       $name=$rows->name;
                ?>
                <p class="pv7"><label><input type="checkbox" name="school[]" class="styled" value="<?php echo $id; ?>"/> <?php echo $name ?></label></p>
                <?php       
                    } 
                ?>  
        </div>
        <div class="w150 h220 bg4 fl mr5 ofh pa15">
        <?php  
        	$query="select * from #__search_year where status='1' order by name asc";
            $db->setQuery($query);
            $row=$db->loadObjectList();
            if($db->getErrorNum()){
                echo $db->stderr();
                return $false;
            }  
        ?>
            <p class="title fs18 ttu"><a href="">Year</a></p>
                <?php
                    for($i=0,$n=count($row);$i<$n;$i++)
				    {
					   $rows=&$row[$i];
                       $id=$rows->id;
                       $name=$rows->name;
                ?>
                <p class="pv7"><label><input type="checkbox" class="styled" name="year[]" value="<?php echo $id; ?>"/> <?php echo $name ?></label></p>
                <?php       
                    } 
                ?>  
        </div>
        <div class="w150 h220 bg5 fl mr5 ofh pa15">
        <?php 
        	$query="select * from #__search_level where status='1' order by name asc";
            $db->setQuery($query);
            $row=$db->loadObjectList();
            if($db->getErrorNum()){
                echo $db->stderr();
                return $false;
            }  
        ?>
            <p class="title fs18 ttu"><a href="">Level</a></p>
                <?php
                    for($i=0,$n=count($row);$i<$n;$i++)
				    {
					   $rows=&$row[$i];
                       $id=$rows->id;
                       $name=$rows->name;
                ?>
                <p class="pv7"><label><input type="checkbox" class="styled" name="level[]" value="<?php echo $id; ?>"/> <?php echo $name ?></label></p>
                <?php       
                    } 
                ?>  
        </div>
        <div class="w150 h220 bg6 fl mr5 ofh pa15">
        <?php
        	$query="select * from #__search_subject where status='1' order by name asc";
            $db->setQuery($query);
            $row=$db->loadObjectList();
            if($db->getErrorNum()){
                echo $db->stderr();
                return $false;
            }  
        ?>
            <p class="title fs18 ttu"><a href="">Subjects</a></p>
                <?php
                    for($i=0,$n=count($row);$i<$n;$i++)
				    {
					   $rows=&$row[$i];
                       $id=$rows->id;
                       $name=$rows->name;
                ?>
                <p class="pv7"><label><input type="checkbox" class="styled" name="subject[]" value="<?php echo $id; ?>"/> <?php echo $name ?></label></p>
                <?php       
                    } 
                ?>   
        </div>
		<input type="submit" name="searchfilter" value="Search" class="buttonS bg7 fr mt10 mr10" />
    <?php
        }
        else{
    ?>
    <?php
        $user = & JFactory::getUser();
        $login=$user->get('guest'); 
        $id_school=JRequest::getVar('school');
        $id_school = implode($id_school, ','); 
        $id_year=JRequest::getVar('year');
		$id_year = implode($id_year, ','); 
        
        $id_level=JRequest::getVar('level');
		$id_level = implode($id_level, ','); 
        
        $id_subject=JRequest::getVar('subject');
		$id_subject = implode($id_subject, ','); 
                
        $query2="select *, sc.name as school_name, sc.descrip as school_description, l.name as level_name, y.name as year_name, s.name as subject_name from #__search_content as c LEFT JOIN #__search_school as sc ON c.id_school = sc.id LEFT JOIN  #__search_level as l ON c.id_level = l.id LEFT JOIN  #__search_year as y ON c.id_year = y.id LEFT JOIN #__search_subject as s ON c.id_subject = s.id where 1=1";
		if(!empty($id_school))
			$query2 .= ' and id_school IN ('.$id_school.')';
		if(!empty($id_year))
			$query2 .= ' and id_year IN ('. $id_year .')';
		if(!empty($id_level))
			$query2 .= ' and id_level IN ('.$id_level.')';
		if(!empty($id_subject))
			$query2 .= ' and id_subject IN('.$id_subject.')';

        $dblink->setQuery($query2);
        $list = $dblink->loadObjectList();       

    ?>
	<?php if(!empty($list)){
			foreach($list as $item)
		{
	?>
		<div class="w145 mr20 taj fl">
			<table width="100%">
				<tr>
					<td class="pb10">YEAR</td>
					<td class="pb10">:<?php echo $item->year_name;?></td>
				</tr>
				<tr>
					<td class="pb10">LEVEL</td>
					<td class="pb10">:<?php echo $item->level_name;?></td>
				</tr>
				<tr>
					<td class="pb10">SUBJECT</td>
					<td class="pb10">:<?php echo $item->subject_name;?></td>
				</tr>
			</table>
		</div>
		<div class="taj ofh">		
			<div class="w385 h100 bg3 mr5 ofh fl pa20">
				<p class="title fs18 ttu"><a href="">School</a></p>
				<p class="pv7"><?php echo $item->school_description;?></p>
			</div>
			
			<div class="w50 h100 bg4 fl mr5 ofh pa20 tac">
				<p class="title fs18 ttu"><a href="">CA 1</a></p>
				<p class="pv7 ofh tac ph15"><input type="checkbox" class="styled" disabled="true" <?php echo $item->ca1 == 'ca1' ? 'checked' : ''; ?> /></p>
			</div>
			<div class="w50 h100 bg5 fl mr5 ofh pa20 tac">
				<p class="title fs18 ttu"><a href="">SA 1</a></p>
				<p class="pv7 ofh tac ph15"><input type="checkbox" class="styled" disabled="true" <?php echo $item->sa1 == 'sa1' ? 'checked' : ''; ?> ></p>
			</div>
			<div class="w50 h100 bg6 fl mr5 ofh pa20 tac">
				<p class="title fs18 ttu"><a href="">CA 2</a></p>
				<p class="pv7 ofh tac ph15"><input type="checkbox" class="styled" disabled="true" <?php echo $item->ca2 == 'ca2' ? 'checked' : ''; ?> /></p>
			</div>
			<div class="w50 h100 bg5 fl mr5 ofh pa20 tac">
				<p class="title fs18 ttu"><a href="">SA 2</a></p>
				<p class="pv7 ofh tac ph15"><input type="checkbox" class="styled" disabled="true" <?php echo $item->sa2 == 'sa2' ? 'checked' : ''; ?> /></p>
			</div>
			<div class="clear"></div>	
			<?php if ($login==0){ ?>
				<input type="button" name="download" value="Download" class="button fr mt10 mr10" onclick="window.open('<?php echo JRequest::getURI().'?act=down&id='. $item->id;?>')" />
			<?php
			}else{?>
			 <input type="button" name="download" value="Download" onclick="return login();" class="buttonS fr mt10 mr10" />   
			<?php 
			}
		}
	}
	else
			echo '<div style="text-align:center">No record founded</div>';
         ?>		
		</div>
    
    <?php
    if (isset($_POST['download']) && $login==0){
        $descrip=$_SESSION['descrip'];
        $nd=$_SESSION['nd'];
        $nd=$nd.$descrip;
        exportpdf($nd);
        }
    }
       
    ?>
    </form> 
</div>

<?php
function exportpdf($content){
    require_once(dirname(__FILE__).DS.'pdf'.DS.'config'.DS.'lang'.DS.'eng.php');
    require_once(dirname(__FILE__).DS.'pdf'.DS.'tcpdf.php');
    
    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    // set document information
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('Nicola Asuni');
    $pdf->SetTitle('TCPDF Example 021');
    $pdf->SetSubject('TCPDF Tutorial');
    $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
    
    // set default header data
    $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
    
    // set header and footer fonts
    $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
    $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
    
    // set default monospaced font
    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
    
    //set margins
    $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
    $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
    $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
    
    //set auto page breaks
    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
    
    //set image scale factor
    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
    
    //set some language-dependent strings
    $pdf->setLanguageArray($l);
    
    // ---------------------------------------------------------
    
    // set font
    $pdf->SetFont('helvetica', '', 9);
    
    // add a page
    $pdf->AddPage();
    
    // create some HTML content
    $html = $content;
    
    // output the HTML content
    $pdf->writeHTML($html, true, 0, true, 0);
    
    // reset pointer to the last page
    $pdf->lastPage();
    $rename=time();
    
    // ---------------------------------------------------------
    
    //Close and output PDF document
    ob_end_clean();
    $pdf->Output("OLA-$rename.pdf", "I");
}
?>

